package chances.webui.taglibs;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import chances.webui.model.Column;
import chances.webui.model.Table;
import chances.webui.model.Validator;

@SuppressWarnings("serial")
public class TableTag extends ToolbarUITag {

	private Table table;
	private String searchForm;
	private String editorForm;

	private String dataload;
	private Map<String,Validator> validators;
	
	@Override
	public void setPageContext(PageContext pageContext) {
		validators = new LinkedHashMap<String,Validator>();
		super.setPageContext(pageContext);
	}	

	@Override
	protected void initContext(Map<String, Object> context) {
			context.put("table", table);
			context.put("lookups",this.getJson(table.getLookups()));
			WebPageContext.getWebPageContext(pageContext).addComponent(table);
	}	
	
	@Override
	protected void reset() {
		this.table = new Table();
		super.reset();
		this.setHeader(null);
		this.setFooter(null);
		this.dataload = null;
	}
	
	@Override
	public int doEndTag() throws JspException {
		int result = super.doEndTag();
		this.validators.clear();
		return result;
	}
	
	public Table getTable() {
		return table;
	}

	public void setId(String id) {
		table.setId(id);
		super.setId(id);
	}

	public void setSearchUrl(String searchUrl) {
		table.setSearchUrl(searchUrl);
	}

	public void setDataUrl(String dataUrl) {
		table.setDataUrl(dataUrl);
	}

	public void setSize(int size) {
		table.setSize(size);
	}

	public void setShowToolbar(boolean showToolbar) {
		table.setShowToolbar(showToolbar);
	}

	public void setShowSearch(boolean showSearch) {
		table.setShowSearch(showSearch);
	}

	public void setShowExport(boolean showExport) {
		table.setShowExport(showExport);
	}

	public void setShowRefresh(boolean showRefresh) {
		table.setShowRefresh(showRefresh);
	}

	public void setShowToggle(boolean showToggle) {
		table.setShowToggle(showToggle);
	}

	public void setStyleClass(String styleClass) {
		table.setStyleClass(styleClass);
	}

	public void setFormUrl(String formUrl) {
		table.setFormUrl(formUrl);
	}


	public void setAppend(boolean append) {
		table.setAppend(append);
	}

	public void setDataType(String dataType) {
		table.setDataType(dataType);
	}

	public void setColumns(List<Column> columns) {
		table.setColumns(columns);
	}

	public void setEditable(boolean editable) {
		table.setEditable(editable);
	}
	
	public boolean isEditable() {
		return this.table.isEditable();
	}

	public void setDatas(String datas) {
		table.setDatas(datas);
	}
	
	public void setTitle(String title) {
		table.setTitle(title);	
	}


	public String getSearchForm() {
		return searchForm;
	}


	public void setSearchForm(String searchForm) {
		this.searchForm = searchForm;
	}


	public boolean isAppend() {
		return this.table.isAppend();
	}


	public String getDataload() {
		return dataload;
	}


	public void setDataload(String dataload) {
		this.dataload = dataload;
	}


	public String getEditorForm() {
		return editorForm;
	}
	
	public void setEditorForm(String editorForm) {
		this.editorForm = editorForm;
	}

	public void addValidator(String field, Validator vaildator) {
		this.validators.put(field, vaildator);
		
	}
	public Map<String, Validator> getValidators() {
		return validators;
	}

	public void setPostUrl(String postUrl) {
		table.setPostUrl(postUrl);
	}
	
}
